🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / repository / src / commonMain / kotlin / org / meshtastic / core / repository / MeshBeaconRepository.kt
Displaying Raw • Download
core/repository/src/commonMain/kotlin/org/meshtastic/core/repository/MeshBeaconRepository.kt bf929c20f274232e0eb8ddc19b2dc82403e99e5b (bf929c20) Text, 3.62 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.repository
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.flow.MutableStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.StateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.asStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.update
Tff7b72import T7ee787kotlinx.coroutines.launch
Tff7b72import T7ee787org.meshtastic.core.model.MeshBeaconOffer
T8b949e/**
* Holds Mesh Beacon invitations received from other meshes. Beacons are advisory, zero-hop advertisements — not
* messages or contacts — so they live in the Discovery surface, which presents them for the user to Discover / Join /
* Dismiss. The list is capped and deduped in memory (the source of truth) and written through to [MeshBeaconPrefs] so
* invitations survive an app restart until the user explicitly dismisses them (Apple `014-mesh-beacons` FR-015).
*
* Note: persisted as opaque delimited records in a Preferences DataStore, not a Room table — the set is tiny (≤
* [MAX_OFFERS]) and never queried. Promote to a Room entity alongside the discovery tables if querying/joins appear.
*/
Tff7b72class T56d364MeshBeaconRepositoryTb4b4b4(Tff7b72private Tff7b72val Te6edf3prefsTb4b4b4: Te6edf3MeshBeaconPrefsTb4b4b4, Te6edf3scopeTb4b4b4: Te6edf3CoroutineScopeTb4b4b4) Tb4b4b4{
Tff7b72private Tff7b72val Te6edf3_offers Tff7b72= Te6edf3MutableStateFlowTff7b72<Te6edf3ListTff7b72<Te6edf3MeshBeaconOfferTff7b72>Tff7b72>Tb4b4b4(Te6edf3emptyListTb4b4b4(Tb4b4b4)Tb4b4b4)
Tff7b72val Te6edf3offersTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3ListTff7b72<Te6edf3MeshBeaconOfferTff7b72>Tff7b72> Tff7b72= Te6edf3_offersTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)
Tff7b72init Tb4b4b4{
T8b949e// Hydrate once from disk. Memory is authoritative afterwards, so a live beacon that arrives before the async
T8b949e// DataStore load wins; the guard also makes our own write-through re-emissions no-ops.
Te6edf3scopeTb4b4b4.Te6edf3launch Tb4b4b4{
Te6edf3prefsTb4b4b4.Te6edf3storedBeaconsTb4b4b4.Te6edf3collect Tb4b4b4{ Te6edf3records Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3recordsTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72returnTf0883e@collect
T8b949e// Fold the empty-guard into the atomic update so a live beacon that arrived first (or a concurrent
T8b949e// add/dismiss) is never clobbered by the async DataStore snapshot.
Te6edf3_offersTb4b4b4.Te6edf3update Tb4b4b4{ Te6edf3current Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3currentTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Te6edf3recordsTb4b4b4.Te6edf3mapNotNullTb4b4b4(Te6edf3MeshBeaconOfferTff7b72::Te6edf3decodeTb4b4b4)Tb4b4b4.Te6edf3takeTb4b4b4(Te6edf3MAX_OFFERSTb4b4b4) Tff7b72else Te6edf3current
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Records a received [offer], replacing any prior offer with the same [key][MeshBeaconOffer.key] (a re-broadcast of
* a standing invitation). Returns true when this is a newly-seen invitation, so the caller can notify only once
* rather than on every periodic re-broadcast.
*/
Tff7b72fun Td2a8ffaddTb4b4b4(Te6edf3offerTb4b4b4: Te6edf3MeshBeaconOfferTb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72val Te6edf3isNew Tff7b72= Te6edf3_offersTb4b4b4.Te6edf3valueTb4b4b4.Te6edf3none Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3key Tff7b72=Tff7b72= Te6edf3offerTb4b4b4.Te6edf3key Tb4b4b4}
Te6edf3_offersTb4b4b4.Te6edf3update Tb4b4b4{ Te6edf3current Tff7b72-Tff7b72> Tb4b4b4(Te6edf3listOfTb4b4b4(Te6edf3offerTb4b4b4) Tff7b72+ Te6edf3currentTb4b4b4.Te6edf3filterNot Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3key Tff7b72=Tff7b72= Te6edf3offerTb4b4b4.Te6edf3key Tb4b4b4}Tb4b4b4)Tb4b4b4.Te6edf3takeTb4b4b4(Te6edf3MAX_OFFERSTb4b4b4) Tb4b4b4}
Te6edf3persistTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3isNew
Tb4b4b4}
Tff7b72fun Td2a8ffdismissTb4b4b4(Te6edf3keyTb4b4b4: Tffa657StringTb4b4b4) Tb4b4b4{
Te6edf3_offersTb4b4b4.Te6edf3update Tb4b4b4{ Te6edf3current Tff7b72-Tff7b72> Te6edf3currentTb4b4b4.Te6edf3filterNot Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3key Tff7b72=Tff7b72= Te6edf3key Tb4b4b4} Tb4b4b4}
Te6edf3persistTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tff7b72private Tff7b72fun Td2a8ffpersistTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3prefsTb4b4b4.Te6edf3setStoredBeaconsTb4b4b4(Te6edf3_offersTb4b4b4.Te6edf3valueTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3encodeTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Tff7b72private Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72const Tff7b72val Te6edf3MAX_OFFERS Tff7b72= T79c0ff2T79c0ff0
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.04s